package com.iOS.controller;

import com.iOS.dto.NoteExecution;
import com.iOS.dto.UserInfoExecution;
import com.iOS.entity.Note;
import com.iOS.enums.NoteEnum;
import com.iOS.enums.UserInfoEnum;
import com.iOS.service.IUserInfoService;
import com.iOS.service.NoteService;
import com.iOS.util.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value="/Note",method ={RequestMethod.POST} )
public class NoteController {
    @Autowired
    private NoteService noteService;
    @Autowired
    private IUserInfoService iUserInfoService;

    @RequestMapping(value = "/add")
    @ResponseBody
    public Map<String,Object> addNote(HttpServletRequest request){
        Map<String,Object> modelMap=new HashMap<String,Object>();
        String noteTitle= HttpServletRequestUtil.getString(request,"noteTitle");
        String noteContext=HttpServletRequestUtil.getString(request,"noteContext");
        String token=HttpServletRequestUtil.getString(request,"token");
         /*获取图片*/
        MultipartResolver MultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        MultipartHttpServletRequest multipartHttpServletRequest =MultipartResolver.resolveMultipart(request);
        MultipartFile img = multipartHttpServletRequest.getFile("notePictures");
        if(token==null){
            modelMap.put("success",false);
            modelMap.put("errMsg","未登录");
        }else{
            try {
                UserInfoExecution userInfo = iUserInfoService.GetUserInfo(token);
                if (userInfo.getState() == UserInfoEnum.SUCCESS.getState()) {
                    Note note = new Note();
                    int userId = userInfo.getUserInfo().getId();
                    note.setUserId(userId);
                    note.setNoteTitle(noteTitle);
                    note.setNoteContext(noteContext);
                    note.setNotePictures(img.getOriginalFilename());
                    NoteExecution noteExecution = noteService.addNote(note);
                    if (noteExecution.getState() == NoteEnum.SUCCESS.getState()) {
                        modelMap.put("success", true);
                    } else {
                        modelMap.put("success", false);
                        modelMap.put("errMsg", noteExecution.getStateinfo());
                    }
                }
            }catch (Exception e){
                modelMap.put("success", false);
                modelMap.put("errMsg", e.getMessage());
            }
        }
        return modelMap;
    }

    @RequestMapping(value = "/{userId}/{Id}/delete")
    @ResponseBody
    public Map<String,Object> deleteNoteByuserId(@PathVariable("Id") int Id){
        Map<String,Object> modelMap=new HashMap<String,Object>();
        try {
            NoteExecution noteExecution=noteService.deleteNoteById(Id);
            if(noteExecution.getState()==NoteEnum.SUCCESS.getState()){
                modelMap.put("success", true);
            }else{
                modelMap.put("success", false);
                modelMap.put("errMsg", noteExecution.getStateinfo());
            }
        }catch (Exception e){
            modelMap.put("success", false);
            modelMap.put("errMsg", e.getMessage());
        }
        return modelMap;
    }

    @RequestMapping(value = "/{userId}/{Id}/update")
    @ResponseBody
    public Map<String,Object> updateNoteById(@PathVariable("userId") int userId,@PathVariable("Id") int Id,HttpServletRequest request){
        Map<String,Object> modelMap=new HashMap<String,Object>();
        String noteTitle= HttpServletRequestUtil.getString(request,"noteTitle");
        String noteContext=HttpServletRequestUtil.getString(request,"noteContext");
        MultipartResolver MultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        MultipartHttpServletRequest multipartHttpServletRequest =MultipartResolver.resolveMultipart(request);
        MultipartFile img = multipartHttpServletRequest.getFile("notePictures");
        try {
            Note note = new Note();
            note.setId(Id);
            note.setUserId(userId);
            note.setNoteTitle(noteTitle);
            note.setNoteContext(noteContext);
            note.setNotePictures(img.getOriginalFilename());
            NoteExecution noteExecution = noteService.updateNoteById(note);
            if (noteExecution.getState() == NoteEnum.SUCCESS.getState()) {
                modelMap.put("success", true);
            } else {
                modelMap.put("success", false);
                modelMap.put("errMsg", noteExecution.getStateinfo());
            }
        }catch (Exception e){
            modelMap.put("success", false);
            modelMap.put("errMsg", e.getMessage());
        }
        return modelMap;
    }

    @RequestMapping(value = "/{userId}/queryAllNote")
    @ResponseBody
    public Map<String,Object>queryAllNote(@PathVariable("userId") int userId,Model model){
        Map<String,Object> modelMap=new HashMap<String,Object>();
        try {
            NoteExecution noteExecution = noteService.queryAllNote(userId);
            if(noteExecution.getState() == NoteEnum.SUCCESS.getState()){
                List<Note> notes = noteExecution.getNotes();
                model.addAttribute("note", notes);
                modelMap.put("success", true);
            }else{
                modelMap.put("success", false);
                modelMap.put("errMsg", noteExecution.getStateinfo());
            }

        }catch (Exception e){
            modelMap.put("success", false);
            modelMap.put("errMsg", e.getMessage());
        }
        return modelMap;
    }
}
